package fct.mpc.utils;

import java.io.BufferedWriter;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class StatManager
{
	protected static ArrayList<Stat> stats = new ArrayList<Stat>();
	
	public static void addStat(String name, int value)
	{
		stats.add(new Stat(name, value));
	}
	
	public static void clearStats()
	{
		stats.clear();
	}
	
	public static boolean hasStats()
	{
		return !stats.isEmpty();
	}
	
	public static ArrayList<Stat> getAllStats()
	{
		return stats;
	}
	
	public static Stat getStat(String name)
	{
		for(Stat stat : stats)
		{
			if(stat.getName().equalsIgnoreCase(name))
				return stat;
		}
		
		return null;
	}

	public static void populateStatManagerByAsset(InputStream inputStream)
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			
		try
		{
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.parse(inputStream);

			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName("stat");
			
			for (int i=0; i<items.getLength(); i++)
			{
				String statName = "";
				int statValue = 0;
				
				Node item = items.item(i);
				NodeList properties = item.getChildNodes();
		
				for(int j=0; j<properties.getLength(); j++)
				{
					Node property = properties.item(j);
					String name = property.getNodeName();
						
					if (name.equalsIgnoreCase("name"))
						statName = property.getFirstChild().getNodeValue();
					else if (name.equalsIgnoreCase("level"))
						statValue = Integer.parseInt(property.getFirstChild().getNodeValue());
				}
					
				StatManager.addStat(statName, statValue);
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}	
	}
	
	public static void populateStatManagerByXML(InputStream ins)
	{
		try
		{
			XmlPullParserFactory factory  = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			
			parser.setInput(ins, null);
			
			int eventType = parser.getEventType();
			boolean done = false;
			
			String statName = "";
			int statValue = 0;
		
			while (eventType != XmlPullParser.END_DOCUMENT && !done)
			{
				String name = null;
				
				switch (eventType)
				{
				case XmlPullParser.START_DOCUMENT:               
					break;           
				case XmlPullParser.START_TAG:
					name = parser.getName();
					
					if (name.equalsIgnoreCase("name"))
						statName = parser.nextText();
					else if (name.equalsIgnoreCase("level"))
						statValue = Integer.parseInt(parser.nextText());
					break;
				case XmlPullParser.END_TAG:       
					name = parser.getName();
					
					if (name.equalsIgnoreCase("stat"))
						StatManager.addStat(statName, statValue);
					else if (name.equalsIgnoreCase("stats"))
						done = true;
					break;        
				} 
			
				eventType = parser.next();
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}
	
	public static void writeStatManagerAsXML(BufferedWriter bw)
	{
		try
		{
			bw.write("<stats>\n");
			
			for(Stat s : StatManager.getAllStats())
				bw.write(s.getAsXML());
			
			bw.write("</stats>");
			bw.flush();
			bw.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		
		StatManager.clearStats();
	}
}